package com.zhao.dota.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhao.dota.contant.TeamConst;
import com.zhao.dota.model.LeagueMatch;
import com.zhao.dota.model.LeagueMatchUser;
import com.zhao.dota.service.LeagueMatchService;
import com.zhao.dota.struct.Result;
import com.zhao.dota.vo.LeagueMatchVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController()
@RequestMapping("/{leagueId}/leagueMatch")
public class LeagueMatchController {

    @Resource
    private LeagueMatchService leagueMatchService;
    @Resource
    private ObjectMapper objectMapper;

    @PostMapping("/save")
    public Result<Integer> addMatch(@PathVariable Integer leagueId, String users, boolean captainMode) throws JsonProcessingException {
        List<LeagueMatchUser> leagueMatchUsers = objectMapper.readValue(users, new TypeReference<List<LeagueMatchUser>>() {
        });
        int count = leagueMatchService.addMatch(leagueId, captainMode, leagueMatchUsers);
        return Result.success(count);
    }

    @GetMapping("/")
    public Result<Page<LeagueMatchVo>> page(@PathVariable Integer leagueId, Page<LeagueMatch> page) {
        Page<LeagueMatchVo> page1 = leagueMatchService.page(leagueId, page);
        return Result.success(page1);
    }

    @PostMapping("/setWinner")
    public Result<Void> setWinner(@PathVariable Integer leagueId, Integer id, TeamConst winner) {
        leagueMatchService.setWinner(leagueId, id, winner);
        return Result.success();
    }

}
